﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common;
using Microsoft.AspNetCore.Mvc;
using WqLunTan.Areas.Manage.Models;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Controllers
{
    /// <summary>
    /// 文章内容控制器
    /// </summary>
    public class ArticleController : BaseController
    {
        public IActionResult Detail(Guid id)
        {
            var key = $"article-detail-{id.ToString("N")}";
            var art = httpCacheHelper.getCache<article>(key);
            if (art == null)
            {
                art = DbContext.article.Where(x => x.is_show == true && x.id == id).SingleOrDefault();
                httpCacheHelper.setCache(key, art, 10);
            }

            if (art == null)
            {
                return NotFound();
            }
            return View(art);
        }

        public IActionResult List(Guid id, string search, int pageIndex = 1, int pageSize = 20)
        {
            var key = $"article-list-cateid";
            var catelist = httpCacheHelper.getCache<List<article_category>>(key);
            if (catelist == null || catelist.Count == 0)
            {
                catelist = DbContext.art_category.Where(x => x.is_show).ToList();
                httpCacheHelper.setCache(key, catelist, 10);
            }
            if (!catelist.Exists(x => x.id == id))
            {
                return NotFound();
            }

            List<article> list = null;
            var page = new PageModel() { CurrentPageIndex = pageIndex };

            var query = DbContext.article.Where(x => x.is_show && x.cateid == id);

            if (string.IsNullOrWhiteSpace(search))
            {
                var keyList = $"article-list-{id.ToString("N")}-{pageIndex}-{pageSize}";
                var pageKey = $"article-list-page-{id.ToString("N")}-{pageIndex}-{pageSize}";
                list = httpCacheHelper.getCache<List<article>>(keyList);
                if (list != null && list.Count > 0)
                {
                    page = httpCacheHelper.getCache<PageModel>(pageKey);
                }
                else
                {
                    page.TotalCount = query.Count();
                    list = query.OrderByDescending(x => x.top).ThenBy(x => x.sort_num).ThenByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
                    httpCacheHelper.setCache(keyList, list, 3);
                    httpCacheHelper.setCache(pageKey, page, 3);
                }
            }
            else
            {
                query = query.Where(x => x.title.Contains(search) || x.keywords.Contains(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.top).ThenBy(x => x.sort_num).ThenByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            var users = new List<WqUser>();
            var uidlist = list.Select(x => x.uid).ToList();
            if (uidlist.Count > 0)
            {
                foreach (var item in uidlist)
                {
                    var userKey = $"article-list-uid-{item.ToString("N")}";
                    var user = httpCacheHelper.getCache<WqUser>(userKey);
                    if (user != null)
                    {
                        users.Add(user);    // 先从缓存中读取
                    }
                    else
                    {
                        users.Clear();
                        break;  // 获取失败
                    }
                }

                if (users.Count != uidlist.Count)
                {
                    users = DbContext.Users.Where(x => uidlist.Contains(x.Id)).ToList();
                    foreach (var item in users)
                    {
                        var userKey = $"article-list-uid-{item.Id.ToString("N")}";
                        httpCacheHelper.setCache(userKey, item, 10);    // 缓存作者的相关信息
                    }
                }
            }

            ViewData["page"] = page;
            ViewData["list"] = list;
            ViewData["cate"] = catelist.Where(x => x.id == id).SingleOrDefault() ?? new article_category();
            ViewData["users"] = users;

            return View();
        }

        /// <summary>
        /// 预测说明
        /// </summary>
        /// <returns></returns>
        public IActionResult DivinationExplain()
        {
            return View();
        }
    }
}